package com.maxuxu.scihome.interceptor;

import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @className: LoginInterceptor
 * @author: Maxuxu
 * @date: 2022/7/6 0:14
 * @description:
 */
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {
    /**
     * 目标方法执行之前拦截
     * request
     * response
     * handler
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        登录检查逻辑，检验身份
        String loginname = (String) request.getSession().getAttribute("loginname");

        if (loginname != null){
//            放行
            return true;
        }
//        拦截，返回登录页
        String requestURI = request.getRequestURI();
        log.info("访问请求{}",requestURI);
        request.getSession().setAttribute("msg","您没有执行权限，请登录！");
        response.sendRedirect("/");
        return false;
    }
}